package com.songtech.uitest.util;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.module.common.LogInAndOutHandler;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class StepParser {

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;

    @Autowired
    private LogInAndOutHandler logInAndOutHandler;

    /**
     *
     * @param step 类似"00"
     * @return
     */
    public boolean parseApproval(String step){
        return "0".equals(step.substring(0,1)); //是否同意
    }

    public Map<String, Boolean> parseRspAndApproval(String step){
        Map<String, Boolean> result = new HashMap<String, Boolean>();

        String t = step.substring(0,1);
        switch (t){
            case "0":
                result.put("approval", true);
                result.put("rspToMobile", true);
                break;
            case "1":
                result.put("approval", false);
                result.put("rspToMobile", true);
                break;
            case "2":
                result.put("approval", true);
                result.put("rspToMobile", false);
                break;
            case "3":
                result.put("approval", false);
                result.put("rspToMobile", false);
                break;
            default:
                result.put("approval", true);
                result.put("rspToMobile", true);
        }
        return result;
    }


    /**
     *
     * @param step 类似"00"
     * @return
     * @throws Exception
     */
    public WebDriver parseDriver(String step) throws Exception{
        int accountNum = Integer.parseInt(step.substring(1)); //大于0会用新账号重新登录
        WebDriver driver = null;
        if(accountNum == 0){
            driver = driverCreater.getMainDriver();
        }
        //判断是否需要重新登录
        if(accountNum > 0 && accountNum != 9){
            driver = driverCreater.getViceDriver();
            logInAndOutHandler.login(driver, accountNum);
        }
        if(accountNum == 9){
            driver = driverCreater.getThirdDriver();
        }

        return driver;
    }


    /**
     * 写这个是为了screenshoot截图时，能拿到当事阶段的webdriver。不需要像parseDriver要重新控制登录，所以重写了一个
     * @param step
     * @return
     * @throws Exception
     */
    public WebDriver getDriver(String step) throws Exception{
        int accountNum = Integer.parseInt(step.substring(1)); //大于0会用新账号重新登录
        WebDriver driver = null;
        if(accountNum == 0){
            driver = driverCreater.getMainDriver();
        }
        if(accountNum > 0 && accountNum != 9){
            driver = driverCreater.getViceDriver();
        }
        if(accountNum == 9){
            driver = driverCreater.getThirdDriver();
        }

        return driver;
    }



    public void teardown(String step) throws Exception{
        //若是辅助账号及浏览器则退出登录
        int accountNum = Integer.parseInt(step.substring(1)); //大于0即是新账号和vice浏览器登录的，要退出
        if(accountNum > 0){
            WebDriver driver = driverCreater.getViceDriver();
            logInAndOutHandler.logout(driver);
        }

    }




}
